﻿--获取百度上线数据
create proc [dbo].[proc_BaiDuOnLine_GetList]
(
	@startIndex int,
	@endIndex int,
	@docount bit,
	@username nvarchar(50),
	@stateId int,
	@customer nvarchar(100)
)
as
	--公司Id
	declare @companyid varchar (4)
	set @companyid = (select companyid from aspnet_Users where username=@username)

	set nocount on
		if(@docount=1)
		begin
			select Id,CompanyId,OrderId,CustomerId,CustomerName,StateId,'' as StateName from BaiDuOnLine baidu where baidu.companyid = @companyid and (case @stateId when 0 then 0 else baidu.stateId end) = @stateId and baidu.CustomerName like '%'+@customer+'%'
		end
		else
		begin
		declare @indextable table(id int identity(1,1),nid int)
		set rowcount @endIndex
		insert into @indextable(nid) select Id from BaiDuOnLine baidu where baidu.companyid = @companyid and (case @stateId when 0 then 0 else baidu.stateId end) = @stateId and baidu.CustomerName like '%'+@customer+'%'
		
		select
			baidu.Id,
			baidu.CompanyId,
			baidu.OrderId,
			baidu.CustomerId,
			baidu.CustomerName,
			baidu.StateId,
			(select ss.title from ProjectState ss where ss.ProjectId=baidu.stateid) as StateName
		from BaiDuOnLine baidu inner join @indextable t on baidu.id=t.nid
		where 
			t.id between @startIndex and @endIndex 
		order by t.id
		end
	set nocount off
